const fs = require("fs");
const cheerio = require("cheerio");
const superagent = require("superagent");
const moment = require("moment");

const weiboURL = "https://s.weibo.com";
const hotSearchURL = weiboURL + "/top/summary?cate=realtimehot";
var num1 = 0;
function getWeiboJson() {
  superagent.get(hotSearchURL, (err, res) => {
    if (err) console.error(err);
    const $ = cheerio.load(res.text);
    let hotList = [];
    $("#pl_top_realtimehot table tbody tr").each(function (index) {
      if (index !== 0) {
        const $td = $(this).children().eq(1);
        const link = weiboURL + $td.find("a").attr("href");
        const text = $td.find("a").text();
        const hotValue = $td.find("span").text();
        const icon = $td.find("img").attr("src")
          ? "https:" + $td.find("img").attr("src")
          : "";
        hotList.push({
          index,
          link,
          text,
          hotValue,
          icon,
        });
      }
    });
    fs.writeFileSync(
      `${(__dirname, ".")}/DataBase/hotSearch.json`,
      JSON.stringify(hotList),
      "utf-8"
    );
    num1++;
    let current_time = moment(Date.now()).format("YYYY-MM-DD HH:mm:ss");
    console.log("Weibo爬取次数：" + num1 + " |time：" + current_time);
  });
}
//微博爬虫模块-end
function toGet() {
  setInterval(getWeiboJson, 5000);
}

module.exports.weiBoHot = toGet;
